package cn.francis.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qfedu.agro.common.ResultInfo;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Name: AccessDeniedHandlerImpl
 * Package: cn.francis.handler
 * Date: 2024/10/26 - 17:00
 * Description:
 *
 * @author Junhui Zhang
 * @version v1.0
 */

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        String jsonResultInfo = new ObjectMapper().writeValueAsString(ResultInfo.error("权限不足"));
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(jsonResultInfo);
    }
}
